﻿using SCG.SolarVengeanceEngine;
using SCG.TurboSprite;
using SCG.WinForms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace SCG.SolarVengeance
{
    public class PulsarSprite : BitmapSprite
    {
        //constructor
        public PulsarSprite(Pulsar pulsar, Bitmap bmp)
            : base(bmp)
        {
            Pulsar = pulsar;

            //create frames
            if (_frames.Count == 0)
            {
                for (int w = bmp.Width; w > 1; w -= 2)
                {
                    Bitmap frame = ImageUtils.ResizeBitmap(bmp, w, bmp.Height, true);
                    _frames.Add(frame);
                }
                for (int w = 2; w <= bmp.Width; w += 2)
                {
                    Bitmap frame = ImageUtils.ResizeBitmap(bmp, w, bmp.Height, true);
                    frame.RotateFlip(RotateFlipType.RotateNoneFlipX);
                    _frames.Add(frame);
                }
            }
        }

        //switch frames
        public override void Process()
        {
            base.Process();
            if (_direction == 1)
            {
                _currentFrame += _advance;
                if ((int)_currentFrame >= _frames.Count)
                    _direction = -1;
            }
            else
            {
                _currentFrame -= _advance;
                if ((int)_currentFrame <= 0)
                    _direction = 1;
            }
            int idx = (int)_currentFrame;
            if (idx < 0)
                idx = 0;
            if (idx >= _frames.Count)
                idx = _frames.Count - 1;
            Bitmap = _frames[idx];
     
        }

        //render if visible
        public override void Render(Graphics g)
        {
            if (!Pulsar.DiscoveredByCurrentPlayer)
                return;
            base.Render(g);
        }

        //The Pulsar
        public Pulsar Pulsar { get; set; }

        //static
        private static List<Bitmap> _frames = new List<Bitmap>();
        private double _advance = RND.NextDouble() * 5.0 + 1.0;
        private double _currentFrame = 0;
        private int _direction = 1;
    }
}
